package com.hf.eagle.web.struts.admin.manager;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.beanutils.DynaBean;
import org.apache.commons.lang.StringUtils;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import com.hf.eagle.domain.Accessory;
import com.hf.eagle.domain.ProductInfo;
import com.hf.eagle.web.base.BaseAction;
import com.hf.eagle.web.base.Pager;
import com.hf.eagle.web.base.SysConstant;

/**
 * @author Xu,XiaoYuan
 * @version 2012.3.18 1:23
 */
public class RecommendProductAction extends BaseAction {

	public ActionForward unspecified(ActionMapping mapping, ActionForm form, HttpServletRequest request,
			HttpServletResponse response) throws Exception {

		return this.edit(mapping, form, request, response);
	}

	public ActionForward edit(ActionMapping mapping, ActionForm form, HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		setNaviStringToScope(request);

		List<ProductInfo> productInfolist = super.getFacade().getProductInfoService().getAllProductWithType(null);
		request.setAttribute("productInfolist", productInfolist);

		return mapping.findForward("input");
	}

	public ActionForward view(ActionMapping mapping, ActionForm form, HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		setNaviStringToScope(request);

		DynaBean dynaBean = (DynaBean) form;
		String product_id = (String) dynaBean.get("product_id");
		String mod_id = (String) dynaBean.get("mod_id");

		ProductInfo productInfo = new ProductInfo();
		productInfo.setProduct_id(Long.valueOf(product_id));
		productInfo = super.getFacade().getProductInfoService().getProductInfo(productInfo);
		if (null != productInfo) {
			Accessory acce = new Accessory();
			acce.setFile_level(SysConstant.SUB_IMG);
			acce.getMap().put("mod_id", mod_id);
			acce.getMap().put("record_id", productInfo.getProduct_id());
			acce = super.getFacade().getAccessoryService().getAccessory(acce);

			productInfo.setAccessory(acce);
		}
		super.copyProperties(form, productInfo);

		dynaBean.set("type1_name_", productInfo.getMap().get("type1_name"));
		dynaBean.set("type2_name_", productInfo.getMap().get("type2_name"));

		return mapping.findForward("view");
	}

	@SuppressWarnings("finally")
	public ActionForward save(ActionMapping mapping, ActionForm form, HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		ProductInfo productInfo = new ProductInfo();
		super.copyProperties(productInfo, form);

		StringBuffer sf = new StringBuffer();
		try {
			super.getFacade().getProductInfoService().modifyProductInfo(productInfo);
			sf.append("1");
		} catch (Exception e) {
			sf.append("0");
		} finally {
			super.renderJson(response, sf.toString());
			return null;
		}
	}

	public ActionForward productSelect(ActionMapping mapping, ActionForm form, HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		// 根据二级查询//ProductInfo 产品型号名称 及图片信息
		DynaBean dynaBean = (DynaBean) form;
		String mod_id = (String) dynaBean.get("mod_id");
		String type2_id = (String) dynaBean.get("type2_id");
		String type2_name = (String) dynaBean.get("type2_name");
		Pager pager = (Pager) dynaBean.get("pager");
		String title = "";

		pager.setPageSize(12);
		ProductInfo productInfo = new ProductInfo();
		productInfo.setIs_del(0);
		if (StringUtils.isNotBlank(type2_id)) {
			productInfo.setProduct_type(Integer.parseInt(type2_id));
			title = type2_name;
		}
		// super.copyProperties(productInfo, form);

		Long recordCount = super.getFacade().getProductInfoService().getProductInfoCount(productInfo);
		pager.init(recordCount, pager.getPageSize(), pager.getRequestPage());
		productInfo.getRow().setFirst(pager.getFirstRow());
		productInfo.getRow().setCount(pager.getLastRow());
		List<ProductInfo> list = super.getFacade().getProductInfoService().getProductInfoPaginatedList(productInfo);
		for (ProductInfo productInfo2 : list) {
			Accessory acce = new Accessory();
			acce.setIs_del(0);
			acce.setFile_level(SysConstant.SUB_IMG);
			acce.getMap().put("mod_id", mod_id);
			acce.getMap().put("record_id", productInfo2.getProduct_id());
			acce = super.getFacade().getAccessoryService().getAccessory(acce);
			productInfo2.setAccessory(acce);
		}
		productInfo.getMap().put("type2_id", type2_id);
		productInfo.getMap().put("type2_name", type2_name);
		productInfo.getMap().put("title", title);
		request.setAttribute("entityList", list);
		request.setAttribute("title", title);

		return new ActionForward("/../hf/admin/manager/RecommendProduct/productSelect.jsp");
	}

}
